<?

class AGForm
{
  var $modelObject, $formName, $datasetField;
  
  function AGForm($formname, &$modelObject)
  {
    $this->formName = $formname;
    $this->modelObject = &$modelObject;
    $methodname = $this->formName.'_fields';
    if (method_exists($modelObject, $methodname))
    {
      $modelObject->{$methodname}($this->items, $this->properties);
      $this->datasetField = getDefault($this->properties['data'], 'data');
      $this->dsource = &$this->modelObject->{$this->datasetField};
    }
    else
    {
      logError('Form error: model "<b>'.get_class($this->modelObject).
        '</b>" has no "<b>'.$methodname.'</b>" method.');
    }
  }
  
  function display($id, &$item)
  {
    global $config;
    $predefs = array();
    $was_found = false;
    $item['name'] = getDefault($item['name'], $id);
    $value = getDefault($this->dsource[$id], $item['default']);
    $attrList = array();
    foreach ($item as $k => $v) 
      if ($k != 'caption')
        $attrList[] = $k.'="'.htmlentities($v).'"';
    $attributes = implode(' ', $attrList);
    $caption = $item['caption'];
    if (isset($this->predefdir))
      $predefs[] = $this->predefdir.'/form.'.$item['type'].'.php';
    $predefs[] = 'themes/'.$config['site.theme'].'/form.'.$item['type'].'.php';
    $predefs[] = 'predef/form.'.$item['type'].'.php';
    foreach ($predefs as $pdef)
    {
      if (file_exists($pdef)) 
      {        
        include($pdef);
        $was_found = true;
      }
    }
    if (!$was_found)
      logError('Form error: predef not found for element type "<b>'.$item['type'].'</b>" in '.implode(' or ', $predefs));
  }

}

?>
